<template>
	<xn-form-container
		:title="formData.id ? '编辑商品' : '增加商品'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="商品名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入商品名称" allow-clear />
			</a-form-item>
			<a-form-item label="商品编码：" name="code">
				<a-input v-model:value="formData.code" :disabled="disable" placeholder="请输入商品编码" allow-clear />
			</a-form-item>
			<a-form-item label="规格：" name="specification">
				<a-input v-model:value="formData.specification" placeholder="请输入规格" allow-clear />
			</a-form-item>
			<a-form-item label="单价：" name="unitPrice">
				<a-input v-model:value="formData.unitPrice" placeholder="请输入单价" allow-clear />
			</a-form-item>
<!--			<a-form-item label="照片：" name="imageUrl">-->
<!--&lt;!&ndash;				<xn-upload v-model:value="formData.imageUrl" />&ndash;&gt;-->
<!--				<xn-upload-->
<!--					v-model:value="formData.imageUrl"-->
<!--					:action="uploadUrl"-->
<!--					:headers="headers"-->
<!--					:data="extraData"-->
<!--					:before-upload="beforeUpload"-->
<!--					@change="handleChange"-->
<!--					accept="image/*"-->
<!--					list-type="picture-card"-->
<!--				>-->
<!--					<div v-if="!formData.imageUrl">-->
<!--						<loading-outlined v-if="uploading"></loading-outlined>-->
<!--						<plus-outlined v-else></plus-outlined>-->
<!--						<div class="ant-upload-text">上传</div>-->
<!--					</div>-->
<!--				</xn-upload>-->
<!--				<img v-if="formData.imageUrl" :src="formData.imageUrl" alt="avatar" style="width: 100%" />-->
<!--			</a-form-item>-->

		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="amsCommodityInfoForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import amsCommodityInfoApi from '@/api/ams/commodity/amsCommodityInfoApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	const disable =ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			disable.value = true
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
		disable.value = false
	}
	// 默认要校验的
	const formRules = {
		code: [required('请输入CODE')],
		name: [required('请输入NAME')],
		specification: [required('请输入SPECIFICATION')],
		unitPrice: [required('请输入UNIT_PRICE')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				amsCommodityInfoApi
					.amsCommodityInfoSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
